//
//  LESettingView.m
//  qqqqqq
//
//  Created by iOSer on 2017/3/16.
//  Copyright © 2017年 iOSer. All rights reserved.
//

#import "LESettingView.h"

@interface LESettingView()
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) NSArray *itemArray;

@end

@implementation LESettingView


-(instancetype)init{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        self.hidden = YES;
        [self setBackgroundColor:HEXColor(0x000000, 0)];
        [self addSubview:self.bgView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
        [self addGestureRecognizer:tap];
        self.itemArray = @[@"重新开始",@"保存进度",@"返回主页"];
        [_bgView makeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self);
            make.top.equalTo(self).offset(-((self.itemArray.count * GGRealValue(25)+(self.itemArray.count+1)*GGRealValue(10))));
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(180), ((self.itemArray.count * GGRealValue(25)+(self.itemArray.count+1)*GGRealValue(10)))));
        }];
        
        for (int i = 0; i<self.itemArray.count; ++i) {
            UIButton *btn = [UIButton new];
            [btn setTitleColor:HEXColor(0x000000, 1) forState:UIControlStateNormal];
            [btn setBackgroundColor:HEXColor(0xffffff, 1)];
            [btn setTitle:self.itemArray[i] forState:UIControlStateNormal];
            btn.tag = 990+i;
            btn.layer.borderColor = HEXColor(0x000000, 1).CGColor;
            btn.layer.borderWidth = 1;
            btn.layer.cornerRadius = GGRealValue(25)/2;
            
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.bgView addSubview:btn];
            //            btn.frame = CGRectMake(20, SCREEN_HEIGHT-GGRealValue(5), (SCREEN_WIDTH-100)/4, GGRealValue(20));
            [btn makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(_bgView);
                make.top.equalTo(_bgView).offset((GGRealValue(25)*i) + (GGRealValue(10)*(i+1)));
                make.size.equalTo(CGSizeMake(GGRealValue(100), GGRealValue(25)));
            }];
        }
    }
    return self;
}

-(void)btnClick:(UIButton *)btn{
    [self.delegate LESetttingBtnClickWithIndex:btn.tag-990];
}
-(void)showLESettingView{
    self.hidden = NO;
    [self.superview layoutIfNeeded];
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.48 initialSpringVelocity:0.6 options:UIViewAnimationOptionCurveEaseInOut animations:^{                                                                                                                                                                                [_bgView remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.equalTo(CGSizeMake(GGRealValue(180), ((self.itemArray.count * GGRealValue(25)+(self.itemArray.count+1)*GGRealValue(10)))));
    }];
     [self.superview layoutIfNeeded];//强制绘制
    } completion:^(BOOL finished) {
        
    }];
}


-(void)dismissView{
    [self.superview layoutIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
        [_bgView remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(-((self.itemArray.count * GGRealValue(25)+(self.itemArray.count+1)*GGRealValue(10))));
            make.centerX.equalTo(self);
            make.size.equalTo(CGSizeMake(GGRealValue(180), ((self.itemArray.count * GGRealValue(25)+(self.itemArray.count+1)*GGRealValue(10)))));
        }];
        [self.superview layoutIfNeeded];//强制绘制
    }completion:^(BOOL finished) {
        self.hidden = YES;
        self.disBlock();
    }];
}
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        [_bgView setBackgroundColor:HEXColor(0xffffff, 1)];
        _bgView.layer.borderColor = HEXColor(0x000000, 1).CGColor;
        _bgView.layer.borderWidth = 1;
    }
    return _bgView;
}
@end
